﻿Ext.define('Twi.QuickAjax', {
    alias: 'widget.twiQuickAjax',
    reguires: [
        'Twi.Msg'
    ],
    statics:
     {
         /**
         * 异步请求数据
         * @url:     (option)  请求路径
         * @success: (required)  成功时，回调函数
         * @params:  (required)  参数
         * @final: (option) 不管成功失败都要执行的函数
         */
         Ajax: function (config) {
             Twi.waitMask = "";
             //如果没配置则默认不显示loadMask
             if (config.loadMask == undefined) {
                 config.loadMask = false;
             }
             if (config.loadMask) {
                 config.maskTarget = config.maskTarget ? config.maskTarget : Ext.getBody();
                 Twi.waitMask = new Ext.LoadMask(config.maskTarget, { msg: "系统正在处理数据，请稍候..." });  //定义
                 Twi.waitMask.show(); //show方法显示
             }
             config.url = (typeof config.url == 'undefined') ? Twi.Url : config.url;
             Ext.Ajax.request({
                 method: 'POST',
                 url: config.url,
                 success: function (response) {
                     //转成对象
                     var obj = Ext.decode(response.responseText);
                     if (obj.success || obj.Success) {
                         if (typeof config.success == "function") config.success(obj);
                     }
                     else {
                         Twi.Msg.Error(null, obj.message || obj.msg);
                     }
                     if (typeof config.final == "function") config.final(obj);
                     if (config.loadMask) {
                         Twi.waitMask.hide();  //hide()方法隐藏
                     }
                 },
                 failure: function (response) {
                     Twi.Msg.Error(null, '异步加载数据失败，错误信息为：' + response.responseText);
                     if (typeof config.final == "function") config.final(obj);
                     if (config.loadMask) {
                         Twi.waitMask.hide();  //hide()方法隐藏
                     }
                 },
                 params: config.params
             });
         } //end Ajax
     }
});
